package controllers;

import play.*;
import play.db.ebean.Transactional;
import play.mvc.*;
import views.html.*;
import models.*;
import static play.data.Form.*;
import play.data.Form;
import java.util.List;
import play.libs.F;
import play.libs.F.Promise;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Football"));
    }

    @Transactional
    public static Promise<Result> handle() {
        Logger.info("'Handle' start");
        
        Promise<Player> promiseOfPlayer = Promise.promise(
            new F.Function0<Player>() {
                public Player apply() {
                    Form<Player> player = form(Player.class).bindFromRequest();
                    player.get().save();
                    List<Player> players = Player.find.where().orderBy("playerId DESC").setMaxRows(1).findList();
                    Logger.info("Player saved");
                    return players.get(0);
                }
            }
        );
        Logger.info("'Handle' end");
        return promiseOfPlayer.map(new F.Function<Player, Result>() {
            public Result apply(Player player) {
                Logger.info("Got player");
                return ok(handlePage.render(player.name, player.accuratePasses, player.goals));
            }
        });
    }

}
